package cn.kgc.musicserver.controller;


import cn.kgc.musicserver.domain.dto.Result;
import cn.kgc.musicserver.domain.dto.ResultUtil;
import cn.kgc.musicserver.domain.entity.Song;
import cn.kgc.musicserver.service.FileStorageService;
import cn.kgc.musicserver.service.ISongService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 歌曲 前端控制器
 * </p>
 *
 * @author kgc
 * @since 2025-06-18
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/song")
public class SongController {
    private final ISongService songService;
    private final FileStorageService fileStorageService;

    /**
     * 分页歌曲
     *
     * @author 王玉林
     * @param pageNo
     * @param size
     * @return
     */
    @GetMapping("page")
    public Result getSongPage(
            @RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
            @RequestParam(value = "size", defaultValue = "5") int size,
            @RequestParam(value = "search", required = false)String search) {
//        System.out.println("pageNo---:"+pageNo);
//        QueryWrapper<Song> wrapper = new QueryWrapper<>();
//        wrapper.like(search != null, "name", search);
//        wrapper.orderByDesc("id");
//        IPage<Song> page = new Page<>(pageNo, size);
//        page = songService.page(page,wrapper);
//        return ResultUtil.returnDataSuccess(page);
        IPage<Song> page = new Page<>(pageNo, size);
        page = songService.page(page, search != null ? new QueryWrapper<Song>().like("name", search) : null);
        return ResultUtil.returnDataSuccess(page);
    }

    /**
     * 歌曲详情
     *
     * @author 王玉林
     * @param id
     * @return
     */
    @GetMapping("detail")
    public Result getSongDetail(Integer id){
        Song song = songService.getById(id);
        return ResultUtil.returnDataSuccess(song);
    }

    /**
     * 删除歌曲
     *
     * @author 王玉林
     * @param id
     * @return
     */
    @DeleteMapping("del")
    public Result delSong(Integer id){
        boolean b = songService.removeById(id);
        if(!b){
            return ResultUtil.returnFail("删除失败");
        }
        return ResultUtil.returnSuccess("删除成功");
    }

    /**
     * 上传歌曲
     *
     * @author 王玉林
     * @param file
     * @return
     */
    @PostMapping("upload-song")
    public Result uploadSong(@RequestParam("file") MultipartFile file){
        try {
            String fileName = fileStorageService.storeFile(file, "song");
            return ResultUtil.returnDataSuccess( "/song/" + fileName);
        } catch (IOException e) {
            return ResultUtil.returnFail(e.getMessage());
        }
    }

    /**
     * 上传图片
     *
     * @author 王玉林
     * @param file
     * @return
     */
    @PostMapping("upload-img")
    public Result uploadImg(@RequestParam("file") MultipartFile file){
        try {
            String fileName = fileStorageService.storeFile(file, "images\\songPic");
            return ResultUtil.returnDataSuccess( "/img/songPic/" + fileName);
        } catch (IOException e) {
            return ResultUtil.returnFail(e.getMessage());
        }
    }

    /**
     * 添加歌曲
     *
     * @author 王玉林
     * @param song
     * @return
     */
    @PostMapping("add")
    public Result addSong(@RequestBody Song song){
        // 更新 createTime
        song.setCreateTime(LocalDateTime.now());
        song.setUpdateTime(LocalDateTime.now());
        boolean b = songService.save(song);
        if(!b){
            return ResultUtil.returnFail("添加失败");
        }
        return ResultUtil.returnSuccess("添加成功");
    }

    /**
     * 修改歌曲
     *
     * @author 王玉林
     * @param song
     * @return
     */
    @PostMapping("update")
    public Result updateSong(@RequestBody Song song){
        // 跟新 updateTime
        song.setUpdateTime(LocalDateTime.now());
        boolean b = songService.updateById(song);
        if(!b){
            return ResultUtil.returnFail("更新失败");
        }
        return ResultUtil.returnSuccess("更新成功");
    }
}
